package api

import (
	"api-getway/basic/config"
	__ "api-getway/basic/proto"
	"api-getway/handler/pkg"
	"api-getway/handler/request"

	"github.com/gin-gonic/gin"
)

func Login(c *gin.Context) {
	var req request.User
	err := c.ShouldBind(&req)
	if err != nil {
		c.JSON(400, gin.H{
			"msg": "参数错误",
		})
		return
	}

	user, err := config.GoodsClient.LoginUser(c, &__.LoginUserReq{
		Username: req.Account,
		Password: req.Password,
	})
	if err != nil {
		c.JSON(500, gin.H{
			"msg": "登录失败",
		})
		return
	}
	//生成token
	newJwt := pkg.NewJWT()
	claims := pkg.CustomClaims{ID: uint(user.Userid)}
	token, _ := newJwt.CreateToken(claims)

	c.JSON(200, gin.H{
		"userId": user.Userid,
		"msg":    "登录成功",
		"token":  token,
	})

}
